package com.woniu.service;

import com.woniu.adapter.MessageRepositoryAdapter;
import com.woniu.service.bean.MessageReceivedCommand;
import com.woniu.service.bean.MessageReceivedCommandHandler;
import com.woniu.service.bean.SendMessageCommand;
import com.woniu.service.bean.SendMessageCommandHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MessageService implements SendMessageCommandHandler, MessageReceivedCommandHandler {

    private final MessageRepositoryAdapter messageRepositoryAdapter;

    public MessageService(MessageRepositoryAdapter messageRepositoryAdapter) {
        this.messageRepositoryAdapter = messageRepositoryAdapter;
    }

    @Override
    public void action(MessageReceivedCommand command) {
        log.info("消息[{}]已被消费者接收、消费", command.getId());
        messageRepositoryAdapter.changeMessageStatusToReceived(command.getId());
    }

    @Override
    public void action(SendMessageCommand command) {
        messageRepositoryAdapter.insert(command);
    }

}
